package com.hzpd.hainan.app;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

public class FragmentVPAdapter<T extends Fragment> extends FragmentPagerAdapter {
	protected List<T> fragments;
	protected FragmentManager fm;
	
	public FragmentVPAdapter(FragmentManager fm) {
		super(fm);
		this.fm = fm;
		this.fragments = new ArrayList<T>();
	}
	
	public void setFragments(List<T> fragments) {
		if(this.fragments != null){
			FragmentTransaction ft = fm.beginTransaction();
			for(Fragment f:this.fragments){
				ft.remove(f);
			}
			ft.commit();
			ft=null;
			fm.executePendingTransactions();
		}
		this.fragments = fragments;
		notifyDataSetChanged();
	}
	
	@Override
	public int getItemPosition(Object object) {
	    return POSITION_NONE;  
	}  

	@Override
	public Fragment getItem(int position) {
		return fragments.get(position);
	}

	@Override
	public int getCount() {
		return fragments.size();
	}

	@Override
	public CharSequence getPageTitle(int position) {
		CharSequence title=fragments.get(position).getTag();
		if(TextUtils.isEmpty(title)){
			title=super.getPageTitle(position);
		}
		return title;
	}
	
	
}
